# 实现用户登录注册注销功能
from rest_framework.views import APIView
from rest_framework.response import Response

from luckin.service.user import (
    handle_update_user,
    handle_login,
    handle_register,
    handle_user_avatar,
    handle_verify_code,
)
from luckin.serialization import UserSerializers
from luckin.authentication import TokenAuthentication
from luckin.common import base64_encode, get_token


# 用户登录控制
class LoginView(APIView):
    http_method_names = ["post"]  # 只支持post方式

    def post(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        flag, data = handle_login(request)
        if flag:
            # 登录成功，设置cookie，返回该用户相应的数据
            # 用户数据
            user_serializers = UserSerializers(data)
            # 返回的cookie
            cookie_arg1 = base64_encode(data.phone)
            token = get_token(cookie_arg1)
            response_data["id"] = cookie_arg1
            response_data["tn"] = token
            response_data["user"] = user_serializers.data
        else:
            response_data["message"] = data
            response_data["code"] = 400

        return Response(status=200, content_type="application/json", data=response_data)


# 用户注册视图
class RegisterView(APIView):
    http_method_names = ["post"]

    def post(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        flag, data = handle_register(request)
        if not flag:
            response_data["message"] = data
            response_data["code"] = 400

        return Response(status=200, content_type="application/json", data=response_data)


# 验证码视图
class VerifyCodeView(APIView):
    http_method_names = ["post"]

    def post(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        flag, data = handle_verify_code(request)
        if not flag:
            response_data["code"] = 400
            response_data["message"] = data
        else:
            response_data["path"] = data

        return Response(status=200, content_type="application/json", data=response_data)


# 用户更新方法
class UserView(APIView):
    # 只能更新用户，只能是用户权力，所以只对put进行参数加密，其他不用加密方法
    http_method_names = ["put"]
    authentication_classes = [TokenAuthentication]

    # 获取所有(单个)用户，仅仅管理员有权利
    def get(self, request, *args, **kwargs):
        pass

    # 创建用户,仅仅管理员有权利
    def post(self, request, *args, **kwargs):
        pass

    # 更新用户，仅仅管理员和用户本人才行
    def put(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        if not handle_update_user(request):
            response_data["code"] = 400
            response_data["message"] = "failure"

        return Response(status=200, content_type="application/json", data=response_data)

    # 删除用户，仅仅管理员使用(用户删除测试用户)
    def delete(self, request, *args, **kwargs):
        pass


# 更新头像
class UserAvatar(APIView):
    http_method_names = ["post"]
    authentication_classes = [TokenAuthentication]

    # 头像上传不需要加密，只需确定是用户本人就行
    def post(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        flag, msg = handle_user_avatar(request)
        if flag:
            response_data["path"] = msg
        else:
            response_data["code"] = 400
            response_data["message"] = msg

        return Response(status=200, content_type="application/json", data=response_data)



